package net.xdclass.service.common.impl;

import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import net.xdclass.config.KafkaTopicConfig;
import net.xdclass.dto.common.CaseInfoDTO;
import net.xdclass.enums.TestTypeEnum;
import net.xdclass.service.common.ResultSenderService;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.stereotype.Service;

import java.util.Objects;


/**
 * 消息发送组件
 * @author 小二
 */
@Service
@Slf4j
public class KafkaSenderServiceImpl implements ResultSenderService {

    @Resource
    private KafkaTemplate<String, String> kafkaTemplate;

    private static final String CASE_ID = "case_id_";

    @Override
    public void sendResult(CaseInfoDTO caseInfoDTO, TestTypeEnum testTypeEnum, String result) {
        switch (testTypeEnum) {
            case STRESS -> sendStressResult(caseInfoDTO, result);
            case API -> sendApiResult(caseInfoDTO, result);
            case UI -> sendUiResult(caseInfoDTO, result);
            default -> {}
        }
    }

    private void sendStressResult(CaseInfoDTO caseInfoDTO, String result) {
        kafkaTemplate.send(KafkaTopicConfig.STRESS_TOPIC_NAME, CASE_ID + caseInfoDTO.getId(), result);
    }

    private void sendApiResult(CaseInfoDTO caseInfoDTO, String result) {
        kafkaTemplate.send(KafkaTopicConfig.API_TOPIC_NAME, CASE_ID + caseInfoDTO.getId(), result);
    }

    private void sendUiResult(CaseInfoDTO caseInfoDTO, String result) {
        kafkaTemplate.send(KafkaTopicConfig.UI_TOPIC_NAME, CASE_ID + caseInfoDTO.getId(), result);
    }
}
